import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Login from '@/views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }
]

const router = new VueRouter({
  routes
  
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");

  if (to.path === '/login') {
    if (token) {
      next({ path: '/' });
    } else {
      next();
    }
  } else {
    if (token) {
      next();
    } else {
      next({ path: '/login' });
    }
  }
});

export default router;
